#!/bin/sh
# created by kezengquan 2013.11.20 for wds connection

wds_scan(){
    	if [ -f /tmp/wds_scan_result.txt ];then
		rm -f /tmp/wds_scan_result.txt
    	fi
    	iwpriv apcli0 set SiteSurvey=1
    	iwpriv apcli0 get_site_survey > /tmp/wds_scan_result.txt
}

wds_stat(){
    	local stat=$(iwconfig apcli0 | grep "Access Point" | awk -F ":" '{print $3}')
    	if [ `echo "$stat" | grep "Not"` ];then
		uci set wireless.wds.stat=0
    	else
		uci set wireless.wds.stat=1
    	fi
		uci commit wireless.wds.stat
}

wds_usage(){
    	echo "Usage:"
    	echo "wds start ssid bssid channel authmode encryptype <key> <keyindex>"
    	echo "wds stat"
    	echo "wds scan"
    	echo "wds stop"
}

#parameters: 	$1:ssid
#		$2:bssid
#		$3:channel
#		$4:authmode
#		$5:encryptype
#		$6:key
#		$7:key index
wds_start(){
	#check whether wifi is enable or not
	if [ "$(uci get wireless.@wifi-iface[0].disabled 2>/dev/null)" = "1" ];then
		exit
	fi
	#check ssid
	if [ $# -lt 5 ];then
		exit
	fi
	if [ `expr length $1` -gt 32 ];then
		echo "ssid format error"
		exit
	fi
    	#check whether mac addr is valid or not
    	regex='[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]'
    	if [ `expr length $1` -eq 17 ]&&[ `echo $2|grep -Eqi $regex\
                && echo YES || echo NO` == "NO" ]; then
		exit
    	fi
	#check channel 
	if [ $3 -lt 1 ] || [ $3 -gt 14 ];then
		echo "channel error"
		exit
	fi
	#check authmode:OPEN SHARED WPAPSK WPA2PSK
	local authmode
	local encryptype
	if [ `echo "$4" | grep "PSK"` ];then
		if [ `echo "$4" | grep "WPA2PSK"` ];then
	    		authmode="WPA2PSK"
		else
	    		authmode="WPAPSK"
		fi
	else
		if [ "$4" = "OPEN" ] || [ "$4" = "SHARED" ];then
			authmode="$4"
		else
			echo "authmode error"
			exit
		fi
	fi
	#check encryptyp:NONE WEP AES TKIP
	if [ `echo "$5" | grep "AES"` ];then
		encryptype="AES"
	elif [ `echo "$5" | grep "TKIP"` ];then
		encryptype="TKIP"
	else
		if [ "$5" = "NONE" ];then
			encryptype="$5"
		elif [ "$5" = "WEP" ];then
			encryptype="$5"
			authmode="WEPAUTO"
		else
			echo "encryptype error"
			exit
		fi
	fi
	#check keyindex:1~4
	if [ "$authmode" = "OPEN" ] && [ "$encryptype" = "NONE" ];then
		echo ""
	else
		if [ $# -lt 6 ];then
			exit
		fi
		if [ "$authmode" = "WPA2PSK" ] || [ "$authmode" = "WPAPSK" ];then
			echo ""
		else
			if [ $# -lt 7 ];then
				exit
			fi
    			if [ $7 -lt 1 ] || [ $7 -gt 4 ]; then
				echo "keyindex error"
				exit
    			fi
		fi
		#check key
		if [ "$authmode" = "OPEN" ] && [ "$encryptype" = "WEP" ];then
			if [ `expr length $6` -ne 5 ] && [ `expr length $6` -ne 13 ] \
				&& [ `expr length $6` -ne 10 ] && [ `expr length $6` -ne 26 ]; then
				echo key length error
				exit
			fi
		elif [ "$authmode" = "SHARED" ] || [ "$authmode" = "WEPAUTO" ];then
			if [ `expr length $6` -ne 5 ] && [ `expr length $6` -ne 13 ] \
				&& [ `expr length $6` -ne 10 ] && [ `expr length $6` -ne 26 ]; then
				echo key length error
				exit
			fi
		elif [ "$authmode" = "WPA2PSK" ] || [ "$authmode" = "WPAPSK" ];then
			if [ `expr length $6` -lt 8 ] || [ `expr length $6` -gt 64 ]; then
				echo "key length error"
                		exit
            		fi
		fi
	fi
	
	#start configuration wireless add save parameters
	ifconfig apcli0 down
        uci set wireless.wds.ssid=$1
        uci set wireless.wds.bssid=$2
        uci set wireless.wds.channel=$3
        uci set wireless.wds.authmode=$authmode
        uci set wireless.wds.encryptype=$encryptype

        iwpriv apcli0 set ApCliEnable=0
        iwpriv apcli0 set ApCliSsid=$1
        iwpriv apcli0 set ApCliBssid=$2
        iwpriv apcli0 set Channel=$3
        iwpriv apcli0 set ApCliAuthMode=$authmode
        iwpriv apcli0 set ApCliEncrypType=$encryptype
	if [ "$authmode" = "OPEN" ] && [ "$encryptype" = "WEP" ];then
		uci set wireless.wds.defaultkeyid=$7
		iwpriv apcli0 set ApCliDefaultKeyID=$7
		if [ `expr length $6` -eq 5 ] || [ `expr length $6` -eq 13 ];then
                	uci set wireless.wds.key=$6
                	iwpriv apcli0 set ApCliKey${7}=$6
		elif [ `expr length $6` -eq 10 ] || [ `expr length $6` -eq 26 ];then
                	uci set wireless.wds.key=$6
                	iwpriv apcli0 set ApCliKey${7}=$6
		fi
	elif [ "$authmode" = "SHARED" ] || [ "$authmode" = "WEPAUTO" ];then
		uci set wireless.wds.defaultkeyid=$7
		iwpriv apcli0 set ApCliDefaultKeyID=$7
		if [ `expr length $6` -eq 5 ] || [ `expr length $6` -eq 13 ];then
                	uci set wireless.wds.key=$6
                	iwpriv apcli0 set ApCliKey${7}=$6
		elif [ `expr length $6` -eq 10 ] || [ `expr length $6` -eq 26 ];then
                	uci set wireless.wds.key=$6
                	iwpriv apcli0 set ApCliKey${7}=$6
		fi
	elif [ "$authmode" = "WPA2PSK" ] || [ "$authmode" = "WPAPSK" ];then
		uci set wireless.wds.key=$6
		iwpriv apcli0 set ApCliWPAPSK=$6
	fi
	iwpriv apcli0 set ApCliEnable=1
        uci set wireless.wds.enable=1
	uci set wireless.@wifi-iface[0].cliname="apcli0"
	
	uci set network.wan.ifname="apcli0"
	uci set network.wan.proto="dhcp"
	uci commit

	ifconfig apcli0 up
	/etc/init.d/network wan_reconn

	#local result=$(grep $1 /tmp/wds_scan_result.txt)
	#local ssid=$(echo $result | awk -F " " '{print $2}')
	#local bssid=$(echo $result | awk -F " " '{print $3}')
	#local authmode=$(echo $result | awk -F " " '{print $4}' | awk -F "/" '{print $1}')
	#local encryptype=$(echo $result | awk -F " " '{print $4}' | awk -F "/" '{print $2}')
}

wds_stop(){
	ifconfig apcli0 down
    	iwpriv apcli0 set ApCliEnable=0
	uci set wireless.wds.stat=0
   	uci set wireless.wds.enable=0
	uci del wireless.@wifi-iface[0].cliname 2>/dev/null

	uci set network.wan.ifname="eth2.2"
	uci commit
}

if [ $# -eq 1 ];then
    case "$1" in
	scan) wds_scan;;
	stat) wds_stat;;
	stop) wds_stop;;
	*) wds_usage;;
    esac
elif [ $# -eq 8 ] && [ "$1" = "start" ];then
    wds_start $2 $3 $4 $5 $6 $7 $8
elif [ $# -eq 7 ] && [ "$1" = "start" ];then
    wds_start $2 $3 $4 $5 $6 $7
elif [ $# -eq 6 ] && [ "$1" = "start" ];then
    wds_start $2 $3 $4 $5 $6
else
    wds_usage
fi
